﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using LapvBussiness;
using LapvWebComponent;
 

namespace LapvRSS
{
    public partial class _Default : System.Web.UI.Page
    {
        public string domain = ConfigLapv.Instance.DomainAdmin;
        public string Title_ = "";
        public int TotalPage = 0;
        public string titlepage = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            int tid = 0; 
            int top = -1;
            if (Request.Params["cateid"] != null)
                tid = int.Parse(Request.Params["cateid"].ToString());
            if (Request.Params["top"] != null)
                int.TryParse(Request.Params["top"].ToString(), out top);
            int currpage = -1;
            int pagesize = -1;
            if (Request.Params["currpage"] != null)
                int.TryParse(Request.Params["currpage"].ToString(), out currpage);
            if (Request.Params["pagesize"] != null)
                int.TryParse(Request.Params["pagesize"].ToString(), out pagesize);
            try
            {
                List<news> lst = null;

                using (lapvwebEntities db = new lapvwebEntities())
                {
                    
                    if (tid >0)
                    {
                        lst = db.news.Include("news_category").Where(en => en.status == 1 && en.news_category.id == tid).OrderByDescending(en => en.datecreate).ToList();
                        titlepage = lst.FirstOrDefault().news_category.name;
                    }
                    else
                    {
                        lst = db.news.Include("news_category").Where(en => en.status == 1).OrderByDescending(en => en.datecreate).ToList();
                    }
                    int totalRows = lst.Count;
                    if (top > 0) lst = lst.Skip(0).Take(top).ToList();
                    else
                    {
                        if (currpage > 0 && pagesize > 0)
                        {
                            int nguyen = totalRows / pagesize;
                            int du = totalRows % pagesize;
                            if (du != 0)
                            {
                                TotalPage = nguyen + 1;
                            }
                            else TotalPage = nguyen;
                            int from = currpage * pagesize - pagesize;

                            lst = lst.Skip(from).Take(pagesize).ToList();
                        }
                    }
                    RepeaterRSS.DataSource = lst;
                    RepeaterRSS.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
